
#pragma once
#include <Windows.h>
#include <WindowsX.h>
#include <gdiplus.h>

#pragma comment(lib,"gdiplus")
using namespace Gdiplus;
class KIrWindow
{

	GdiplusStartupInput gdiplusStartupInput;
	ULONG_PTR           gdiplusToken;

	Image* FromID(LPCTSTR strRsrcID,LPCTSTR RsrcTypeName,HINSTANCE hRsrcMod=NULL)
	{

		HRSRC hRsrc=::FindResource(hRsrcMod,strRsrcID,RsrcTypeName);
		SIZE_T size=SizeofResource(hRsrcMod,hRsrc);
		HGLOBAL hglbResrc=LoadResource(hRsrcMod,hRsrc);
		const void* pRsrcData=LockResource(hglbResrc);
		HGLOBAL hBuffer=GlobalAlloc(GMEM_FIXED,size);
		void* pBuffer=NULL;
		pBuffer=::GlobalLock(hBuffer);
		if (pBuffer==NULL)
		{
			return nullptr;
		}
		pBuffer=(BYTE*)memcpy(pBuffer,pRsrcData,SizeofResource(hRsrcMod,hRsrc));
		IStream *pstream=NULL;
		::CreateStreamOnHGlobal(hBuffer,FALSE,&pstream);
		if (pstream==NULL)
		{
			return nullptr;
		}
		Image *pImage=NULL;
		pImage=Image::FromStream(pstream);
		GlobalUnlock(hBuffer);
		pstream->Release();
		::FreeResource(hglbResrc);
		return pImage;
	}
public:
	KIrWindow()
		:gdiplusToken(0UL)

	{
		GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
	}
	~KIrWindow()
	{
		
		if (gdiplusToken!=0)
		{
			GdiplusShutdown(gdiplusToken);
		}
	}

	BOOL SetIrregularWindow(HWND hWnd,LPCTSTR strIdOrFileName,LPCTSTR RsrcTypeName=NULL,HINSTANCE hRsrcMod=NULL)
	{
		DWORD dwStyle=::GetWindowLong(hWnd,GWL_EXSTYLE);
		if ((dwStyle&WS_EX_LAYERED)!=WS_EX_LAYERED)
		{
			::SetWindowLong(hWnd,GWL_EXSTYLE,WS_EX_LAYERED^dwStyle);
		}
		
		HDC dcScreen;
		HDC dcMemory;

		dcScreen=GetDC(NULL);
		dcMemory=CreateCompatibleDC(dcScreen);
		//////////////////////////////////////////////////////////////////////////
		Image *pImg;
		if (RsrcTypeName==NULL)
		{
			pImg=Image::FromFile(strIdOrFileName);
		}
		else
		{
			pImg=FromID(strIdOrFileName,RsrcTypeName,hRsrcMod);
		}

		if (pImg==NULL)
		{
		return FALSE;
		}
	

		HBITMAP bmp=::CreateCompatibleBitmap(dcScreen,pImg->GetWidth(),pImg->GetHeight());

		HBITMAP hOldBitmap= SelectBitmap(dcMemory,bmp);

		Graphics e(dcMemory);

		e.DrawImage(pImg,0,0,pImg->GetWidth(),pImg->GetHeight());
		
		RECT rectDlg;
		::GetWindowRect(hWnd,&rectDlg);

		// calculate the new window position/size based on the bitmap size
		POINT ptWindowScreenPosition={rectDlg.left,rectDlg.top};
		SIZE szWindow={pImg->GetWidth(),pImg->GetHeight()};
		::SetWindowPos(hWnd,HWND_TOP,ptWindowScreenPosition.x,ptWindowScreenPosition.y,
		szWindow.cx,szWindow.cy,SWP_FRAMECHANGED);

		// Perform the alpha blend
		// setup the blend function
		BLENDFUNCTION blendPixelFunction= { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
		POINT ptSrc={0,0}; // start point of the copy from dcMemory to dcScreen

		// perform the alpha blend
		BOOL bRet= ::UpdateLayeredWindow( hWnd,dcScreen, &ptWindowScreenPosition, &szWindow, dcMemory,
			&ptSrc, RGB(255,0,255), &blendPixelFunction, ULW_ALPHA);

		// clean up
		SelectBitmap(dcMemory,hOldBitmap);
		DeleteObject(bmp);
		if (pImg!=NULL)
		{
			delete pImg;
		}
		return bRet;
	}
	
	//////////////////////////////////////////////////////////////////////////
	
};